﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;

class SqlCeManager : IDatabase
{
    SqlCeConnection Connection;

    public SqlCeManager(string ConnectionString)
    {
        Connection = new SqlCeConnection(ConnectionString);
    }

    public void Open()
    {
        Connection.Open();
    }

    public void Close()
    {
        Connection.Close();
    }

    public bool IsOpen()
    {
        return (Connection.State.ToString().CompareTo("Open") == 0) ? true : false;
    }

    public void ExecuteNonQuery(string Query)
    {
        SqlCeCommand Command = new SqlCeCommand(Query, Connection);

        Command.ExecuteNonQuery();
    }

    public string[][] ExecuteReader(string Query)
    {
        SqlCeCommand Command = new SqlCeCommand(Query, Connection);

        SqlCeDataReader Reader = Command.ExecuteReader();

        List<List<string>> DataList = new List<List<string>>();

        int n = 0;

        while (Reader.Read())
        {
            DataList.Add(new List<string>());

            for (int i = 0; i < Reader.FieldCount; i++)
            {
                DataList[n].Add(Reader[i].ToString());
            }
            n++;
        }

        Reader.Close();

        string[][] Data = new string[n][];

        for (int i = 0; i < n; i++)
        {
            Data[i] = DataList[i].ToArray();
        }

        return Data;
    }

    public int ExecuteScalar(string Query)
    {
        SqlCeCommand Command = new SqlCeCommand(Query, Connection);

        return Int32.Parse(Command.ExecuteScalar().ToString());
    }

    public DataSet ReadData(string Query)
    {
        SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(Query, Connection);

        DataSet Data = new DataSet();

        dataAdapter.Fill(Data);

        return Data;
    }
}

